package kassiopeia.gui;

import static org.fest.assertions.Assertions.assertThat;
import java.util.List;
import kassiopeia.commons.log.Log;
import org.junit.Before;
import org.junit.Test;
import kassiopeia.gui.utils.LogsBuffer;
import grails.test.GrailsUnitTestCase;

class LogsBufferTest {

    static final int QUEUE_SIZE = 5

    private LogsBuffer buffer

    @Before
    void setup() {
        buffer = new LogsBuffer(QUEUE_SIZE)

        for(i in 1..9) {
            buffer.offer(log())
        }
    }

    @Test
    void poolAllWhenNoElementsInTest() {
        // given
        LogsBuffer buffer = new LogsBuffer(QUEUE_SIZE)

        // when
        List<Log> results = buffer.pollAll()

        // then
        assertThat(results).isEmpty()
    }

    @Test
    void shouldPollAllElementsTest() {
        // when
        List<Log> results = buffer.pollAll()

        // then
        assertThat(results).hasSize(5).containsSequence(log(5), log(6), log(7), log(8), log(9))
    }

    @Test(expected=IllegalArgumentException.class)
    void poolFromWithNegativeIdTest() {
        // when
        buffer.pollFrom(-1)
    }

    @Test
    void poolFromWhenNoElementsInTest() {
        // given
        LogsBuffer buffer = new LogsBuffer(QUEUE_SIZE)

        // when
        List<Log> results = buffer.pollFrom(5)

        // then
        assertThat(results).isEmpty()
    }

    @Test
    void poolFromWithFutureIdTest() {
        // when
        List<Log> results = buffer.pollFrom(10)

        // then
        assertThat(results).isEmpty()
    }

    @Test
    void poolFromWithTopBorderIdTest() {
        // when
        List<Log> results = buffer.pollFrom(9)

        // then
        assertThat(results).hasSize(1).containsExactly(log(9))
    }

    @Test
    void poolFromWithBottomBorderIdTest() {
        // when
        List<Log> results = buffer.pollFrom(5)

        // then
        assertThat(results).hasSize(5).containsSequence(log(5), log(6), log(7), log(8), log(9))
    }

    @Test
    void poolFromWithIdFromPastTest() {
        // when
        List<Log> results = buffer.pollFrom(4)

        // then
        assertThat(results).hasSize(5).containsSequence(log(5), log(6), log(7), log(8), log(9))
    }

    @Test
    void poolFromWith3ElementsTest() {
        // given
        LogsBuffer buffer = new LogsBuffer(QUEUE_SIZE)
        buffer.offer(log())
        buffer.offer(log())
        buffer.offer(log())

        // when
        List<Log> results = buffer.pollFrom(2)

        // then
        assertThat(results).hasSize(2).containsSequence(log(2), log(3))
    }

    static Log log(long id) {
        return new Log(id)
    }

    static Log log() {
        return new Log(-1)
    }
}
